package com.my.study.proxy;

import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author: yidujun
 * @create: 2021/07/12 10:25
 */
public class RealMethodProxy implements InvocationHandler, Serializable {
    private static final long serialVersionUID = -1229913471515400541L;

    private RealInvokeMethod realInvokeMethod;

    public RealMethodProxy() {
    }

    public RealMethodProxy(RealInvokeMethod realInvokeMethod) {
        this.realInvokeMethod = realInvokeMethod;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String nameInterface = method.getName();
        boolean flag = false;
        Method[] methods = realInvokeMethod.getClass().getMethods();
        for (Method realMethod : methods) {
            String realMethodName = realMethod.getName();
            if (nameInterface.equals(realMethodName)) {
                flag = true;
                break;
            }
        }
        if (flag) {
            System.out.println("=============before===============");
            realInvokeMethod.invoke();
            System.out.println("=============after===============");
        }
        return null;
    }
}
